#include "StdAfx.h"
#include "SvrAddrMgr.h"

BOOL CSvrAddrMgr::AddSvrAddr(STRU_SVR_ADDR_INFO* apAddr, int anCount)
{
	CCriticalAutoLock loAutoLock(m_oCSAddr);
	m_oMapSvrAddr.clear();
	for(int nIndex=0; nIndex<anCount&&nIndex<25; nIndex++)
	{
		if (enum_svr_type_cms == apAddr[nIndex].m_wSvrType)
		{
			m_oMapSvrAddr.push_back(apAddr[nIndex]);
		}
	}
	return TRUE;
}

void CSvrAddrMgr::GetSvrAddrList(std::vector<STRU_SVR_ADDR_INFO>& ao)
{
	CCriticalAutoLock loAutoLock(m_oCSAddr);
	ao = m_oMapSvrAddr;
}

STRU_SVR_ADDR_INFO CSvrAddrMgr::GetRandSvrAddr()
{
	STRU_SVR_ADDR_INFO loAddr;
	CCriticalAutoLock loAutoLock(m_oCSAddr);
	if (m_oMapSvrAddr.empty())
	{
		loAddr.m_lSvrID = 0;
		return loAddr;
	}
	size_t stSize = m_oMapSvrAddr.size();
	return m_oMapSvrAddr[rand()%stSize];
}
